//
//  SLHomeSlidMenuView.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/10.
//

#import "SLDeveloperVC.h"
#import "SLDeviceSettingCell.h"

static  NSString *const kCellId = @"SLDeviceSettingCell";
@interface SLDeveloperVC ()<UITableViewDelegate,UITableViewDataSource>{

}

@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, assign)BOOL isFirstLoad;
@end

@implementation SLDeveloperVC

-(void)viewDidLoad{
    [super viewDidLoad];
    [self configDatas];
    [self setUpSubViews];
}



//根据设备数据刷新页面还是服务端数据
-(void)refresSubViews{
    [self configDatas];
    [_tableView reloadData];
}

-(void)setUpSubViews{
    self.title = kLocalString(@"开发者模式");
    [self.view addSubview:self.tableView];
}

#pragma mark -
#pragma mark - ================<修改配置>===============
-(void)switchSettingWith:(NSInteger)isOpen model:(SLDeviceSettingModel *)model{
    NSString *title = model.title;
    if ([title isEqualToString:@"调试模式"]) {
        [[NSUserDefaults standardUserDefaults] setObject:kSTRING(@(isOpen)) forKey:@"openDebugModel"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kOpenDebugModelChange" object:nil];
        [self refresSubViews];
    }else if ([title isEqualToString:kLocalString(@"简化日志")]){
        [[NSUserDefaults standardUserDefaults] setObject:kSTRING(@(isOpen)) forKey:@"openDebugLogLevel"];
        [self refresSubViews];
    }
}

-(void)configDatas{
    [self.dataArray removeAllObjects];
    NSMutableArray *section1 = [NSMutableArray new];
    NSArray *titles1 = @[kLocalString(@"调试模式"),@"简化日志"];
    NSString *logLevel = kSTRING([[NSUserDefaults standardUserDefaults] objectForKey:@"openDebugLogLevel"]);
    [titles1 enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL * _Nonnull stop) {
        SLDeviceSettingModel *model = [SLDeviceSettingModel new];
        model.title = title;
        model.style = 2;
        if ([title isEqualToString:@"调试模式"]) {
            model.switchValue = [ZCTool isOpenDebugModel] ? @"1":@"0";
            model.desc = @"支持查看日志，可以搜索已绑定的设备等";
        }else if ([title isEqualToString:kLocalString(@"简化日志")]){
            model.switchValue = logLevel;
            model.desc = @"接口日志参数和返回不显示";
        }
        [section1 addObject:model];
    }];
    [_dataArray addObject:section1];
}

#pragma mark - ================<tableViewDataSource>===============
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *sections = _dataArray[section];
    return sections.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SLDeviceSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    SLDeviceSettingModel *model = _dataArray[indexPath.section][indexPath.row];
    cell.model = model;
    NSArray *sections = _dataArray[indexPath.section];
    cell.line.hidden = indexPath.row == sections.count-1;
    CGFloat height = [SLDeviceSettingCell cellHeightWith:model];
    [cell.contentView setRoundedCorners:0 forCorners:UIRectCornerAllCorners frame:CGRectMake(0, 0, kScreenWidth-40, height)];
    if (sections.count == 1) {
        [cell.contentView setRoundedCorners:20 forCorners:UIRectCornerAllCorners frame:CGRectMake(0, 0, kScreenWidth-40, height)];
    }else{
        if (indexPath.row == sections.count-1) {
            [cell.contentView setRoundedCorners:20.0 forCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight frame:CGRectMake(0, 0, kScreenWidth-40, height)];
        }else if (indexPath.row == 0){
            [cell.contentView setRoundedCorners:20.0 forCorners:UIRectCornerTopLeft | UIRectCornerTopRight frame:CGRectMake(0, 0, kScreenWidth-40, height)];
        }
    }
    kWeakSelf(self);
    [cell setSwitchBlock:^(NSInteger isOpen) {
        [weakself switchSettingWith:isOpen model:model];
    }];
    
    return cell;
}

#pragma mark - ================<UITableViewDelegate>===============
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    SLDeviceSettingModel *model = _dataArray[indexPath.section][indexPath.row];
    return [SLDeviceSettingCell cellHeightWith:model];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UITableViewHeaderFooterView *headview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
    UILabel *label = [headview viewWithTag:100];
    if (!headview) {
        headview = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"head"];
        headview.frame = CGRectMake(0, 0, kScreenWidth, 50);
        if (!label) {
            label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, kScreenWidth-80, 15)];
            label.textColor =  kHexColor(@"#4D4D4D");
            label.font = kFont(14);
            label.tag = 100;
            [headview addSubview:label];
        }
    }
    NSArray *sections = _dataArray[section];
    SLDeviceSettingModel *model = sections[0];
    label.text = model.sectionName;
    return headview;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"foot"];
    if (!footerView) {
        footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"foot"];
        footerView.frame = CGRectMake(0, 0, kScreenWidth, 10);
        footerView.contentView.backgroundColor = KClearColor;
    }
    return footerView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SLDeviceSettingModel *model = _dataArray[indexPath.section][indexPath.row];
}

#pragma mark - ================<懒加载>===============
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, SafeAreaTopHeight, kScreenWidth-40, kScreenHeight-SafeAreaTopHeight-SafeAreaBottomHeight) style:UITableViewStyleGrouped];
        _tableView.estimatedRowHeight = 50;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:kCellId bundle:nil] forCellReuseIdentifier:kCellId];
        _tableView.backgroundColor = KClearColor;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

@end
